<?php
/**
* 名称：通用信息模块控制器。
* 描述：通用信息模块相关的控制响应程式。
*/
final class Controller_Universal extends Controller{
    /**
    * 描述：列表视图程式。
    * @access protected
    * @return void
    */
    protected function Listview(){
        $type=Route::GetArguments('type');
        $class=Route::GetArguments('class');
        //~
        $data=array(
            'meta'=>$this->GetMeta($type,$class),
            'base'=>self::$Database->GetLimit(
                'SELECT * FROM '.self::$Database->Table('news').' WHERE class=? ORDER BY id DESC',
                Route::GetArguments('page'),
                20,
                array($class)
            ),
            'urlArguments'=>array(
                'type'=>$type,
                'class'=>$class
            )
        );
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('universal/list.html');
    }

    /**
    * 描述：详细信息程式。
    * @access protected
    * @return void
    */
    protected function Detail(){
        $id=Route::GetArguments('id');
        $type=Route::GetArguments('type');
        $class=Route::GetArguments('class');
        //~
        $data=array(
            'meta'=>$this->GetMeta($type,$class),
            'base'=>array(
                'current'=>self::$Database->GetRow(
                    'SELECT * FROM '.self::$Database->Table('news').' WHERE id=?',
                    array($id)
                ),
                'previous'=>self::$Database->GetRow(
                    'SELECT * FROM '.self::$Database->Table('news').' WHERE id>? AND class=? ORDER BY id ASC',
                    array($id,$class)
                ),
                'next'=>self::$Database->GetRow(
                    'SELECT * FROM '.self::$Database->Table('news').'  WHERE id<? AND class=? ORDER BY id DESC',
                    array($id,$class)
                )
            ),
            'urlArguments'=>array(
                'id'=>$id,
                'type'=>$type,
                'class'=>$class
            )
        );
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('universal/detail.html');
    }

    /**
    * 描述：获取页面META。
    * @access private
    * @param  int     $class 必选 新闻类别参数值。
    * @return array
    */
    private function GetMeta($type,$class){
        $meta=array();
        //~
        switch($type){
            case 'news':
                $meta['title1']='新闻中心';
                $meta['title1_link']=Route::MakeHyperlink(':universal::listview',array('type'=>'news','class'=>'2'));
            break;
            case 'ts':
                $meta['title1']='技术服务';
                $meta['title1_link']=Route::MakeHyperlink(':universal::listview',array('type'=>'ts','class'=>'4'));
            break;
        }
        //~
        switch((int)$class) {
            case 1:
                $meta['title2']='公司新闻';
            break;
            case 2:
                $meta['title2']='行业新闻';
            break;
            case 4:
                $meta['title2']='产品应用';
            break;
            case 6:
                $meta['title2']='实用技术';
        }
        //~
        $meta['title2_link']=Route::MakeHyperlink(
            ':universal::listview',array('type'=>$type,'class'=>$class)
        );
        //~
        return $meta;
    }
}